<template>
    <el-collapse-item v-if="enable" :name="name">
    <template #title>
        <span style="padding-left:20px">
          {{title}}<el-icon class="header-icon"><info-filled /></el-icon>
        </span>
     </template>
    <Message
          v-if="enableMessage"
          :element="element"
          :bpmnModeler="bpmnModeler"
          :timerEventDefinition="timerEventDefinition"
        />
    <Signal
          v-if="enableSignal"
          :element="element"
          :bpmnModeler="bpmnModeler"
          :timerEventDefinition="timerEventDefinition"
        />
     <Message
          v-if="enableReciveTask"
          :element="element"
          :bpmnModeler="bpmnModeler"
          :timerEventDefinition="timerEventDefinition"
        />
     <Error
          v-if="enableError"
          :element="element"
          :bpmnModeler="bpmnModeler"
          :errorEventDefinition="errorEventDefinition"
        />
    <Escalation
          v-if="enableEscalation"
          :element="element"
          :bpmnModeler="bpmnModeler"
          :timerEventDefinition="timerEventDefinition"
        />
    <Timer
          v-if="enableTime"
          :element="element"
          :bpmnModeler="bpmnModeler"
          :timerEventDefinition="timerEventDefinition"
        />
     <Compensation
          v-if="enableCompensationEvents"
          :element="element"
          :bpmnModeler="bpmnModeler"
          :timerEventDefinition="timerEventDefinition"
        />
  </el-collapse-item>
</template>
<script>
import Message from './parts/MessageEventDefinition.vue'
import Signal from './parts/SignalEventDefinition.vue'
import Error from './parts/ErrorEventDefinition.vue'
import Escalation from './parts/EscalationEventDefinition.vue'
import Timer from './parts/TimerEventDefinition.vue'
import Compensation from './parts/CompensateEventDefinition.vue'
import { is } from 'jeeplus-bpmn/lib/util/ModelUtil'
import eventDefinitionHelper from 'jp-bpmn-js-properties-panel/lib/helper/EventDefinitionHelper'
export default {
  components: {
    Message,
    Signal,
    Error,
    Escalation,
    Timer,
    Compensation
  },
  props: {
    element: {
      type: Object,
      default: undefined
    },
    bpmnModeler: {
      type: Object,
      default: undefined
    },
    name: ''
  },
  data () {
    return {
      title: '',
      timerEventDefinition: null
    }
  },
  computed: {
    enableMessage () {
      var events = [
        'bpmn:StartEvent',
        'bpmn:EndEvent',
        'bpmn:IntermediateThrowEvent',
        'bpmn:BoundaryEvent',
        'bpmn:IntermediateCatchEvent'
      ]
      let enable = false
      events.forEach((item) => {
        if (is(this.element, item)) {
          var messageEventDefinition = eventDefinitionHelper.getMessageEventDefinition(this.element)
          if (messageEventDefinition) {
            enable = true
          }
        }
      })
      return enable
    },
    enableSignal () {
      var events = [
        'bpmn:StartEvent',
        'bpmn:EndEvent',
        'bpmn:IntermediateThrowEvent',
        'bpmn:BoundaryEvent',
        'bpmn:IntermediateCatchEvent'
      ]
      let enable = false
      events.forEach((item) => {
        if (is(this.element, item)) {
          var signalEventDefinition = eventDefinitionHelper.getSignalEventDefinition(this.element)
          if (signalEventDefinition) {
            enable = true
          }
        }
      })
      return enable
    },
    enableReciveTask () {
      let enable = false
      if (is(this.element, 'bpmn:ReceiveTask')) {
        enable = true
      }
      return enable
    },
    enableError () {
      var errorEvents = [
        'bpmn:StartEvent',
        'bpmn:BoundaryEvent',
        'bpmn:EndEvent'
      ]
      let enable = false
      errorEvents.forEach((item) => {
        if (is(this.element, item)) {
          this.errorEventDefinition = eventDefinitionHelper.getErrorEventDefinition(this.element)
          if (this.errorEventDefinition) {
            enable = true
          }
        }
      })
      return enable
    },
    enableEscalation () {
      var escalationEvents = [
        'bpmn:StartEvent',
        'bpmn:BoundaryEvent',
        'bpmn:IntermediateThrowEvent',
        'bpmn:EndEvent'
      ]
      let enable = false
      escalationEvents.forEach((item) => {
        if (is(this.element, item)) {
          // var showEscalationCodeVariable = is(this.element, 'bpmn:StartEvent') || is(this.element, 'bpmn:BoundaryEvent')
      // get business object
          var escalationEventDefinition = eventDefinitionHelper.getEscalationEventDefinition(this.element)
          if (escalationEventDefinition) {
            enable = true
          }
        }
      })
      return enable
    },
    enableTime () {
      let enable = false
      var timerEvents = [
        'bpmn:StartEvent',
        'bpmn:BoundaryEvent',
        'bpmn:IntermediateCatchEvent'
      ]
      timerEvents.forEach((item) => {
        if (is(this.element, item)) {
          this.timerEventDefinition = eventDefinitionHelper.getTimerEventDefinition(this.element)
          if (this.timerEventDefinition) {
            this.title = '边界时间属性设置'
            enable = true
          }
        }
      })
      return enable
    },
    enableCompensationEvents () {
      var compensationEvents = [
        'bpmn:EndEvent',
        'bpmn:IntermediateThrowEvent'
      ]
      let enable = false
      compensationEvents.forEach((item) => {
        if (is(this.element, item)) {
          var compensateEventDefinition = eventDefinitionHelper.getCompensateEventDefinition(this.element)
          if (compensateEventDefinition) {
            enable = true
          }
        }
      })
      return enable
    },
    enable () {
      return this.enableMessage || this.enableSignal || this.enableReciveTask || this.enableError || this.enableEscalation || this.enableTime || this.enableCompensationEvents
    }
  }
}
</script>